# connect to google
from pytrends.request import TrendReq
pytrends = TrendReq(hl='en-US', tz=360) #hl is a language selection, tz is used for timezone
kw_list = ["machine learning"] # list of keywords to get data
pytrends.build_payload(kw_list, cat=0, timeframe='today 12-m')
Build Payload is a function which is used to find the trends of all the words we enter
# build payload
# we will pass any word in the kw_list to see the search of the word with time
# For Example we are passing the word 'machine learning'
kw_list = ["machine learning"] # list of keywords to get data
pytrends.build_payload(kw_list, cat=0, timeframe='today 12-m')
#1 Interest over Time
data = pytrends.interest_over_time() # this function will get all the historical data of the entered word
data = data.reset_index()
import plotly.express as px
fig = px.line(data, x="date", y=['machine learning'], title='Keyword Web Search Interest Over Time')
fig.show()
We can pass more than one keyword so see the comparison for example we are passing the following keyword to see their comparison
kw_list = ["machine learning","data science","deep learning"]
pytrends.build_payload(kw_list, cat=0, timeframe='today 12-m')
data = pytrends.interest_over_time() # this function will get all the historical data of the entered word
data = data.reset_index()
import plotly.express as px
fig = px.line(data, x="date", y=["machine learning","data science","deep learning"] , title='Keyword Web Search Interest Over Time')
fig.show()
as you can see that the deep learning is out of competition, the real competition is between machine learning and data science
Similarly lets check some major companies
kw_list = ['apple','samsung','hawaii','vivo']
pytrends.build_payload(kw_list, cat=0, timeframe='today 12-m')
data = pytrends.interest_over_time() # this function will get all the historical data of the entered word
data = data.reset_index()
fig = px.line(data, x="date", y= kw_list , title='Keyword Web Search Interest Over Time')
fig.show()
As you can see the Apple and Samsung are the best competitors, Hawaii is somehow trying to compete but Vivo is out of it
Now let's give some comparison of Bollywood Actors
kw_list = ['Shah Rukh Khan','Salman Khan','Akshay Kumar','Aamir Khan','Hrithik Roshan']
pytrends.build_payload(kw_list, cat=0, timeframe='today 12-m')
data = pytrends.interest_over_time() # this function will get all the historical data of the entered word
data = data.reset_index()
fig = px.line(data, x="date", y= kw_list , title='Keyword Web Search Interest Over Time')
fig.show()
wooo, I was thinking that Shah Rukh Khan will be on top, but in jan 2022 Salman Khan was on trending
If you are interested in the hourly interest of the keyword, you can use the get_historical_intereset() method to fetch hourly data according to the time you have specified.
pytrends.get_historical_interest(kw_list, year_start=2021, month_start=9, day_start=1,
hour_start=0, year_end=2021, month_end=9,
day_end=30, hour_end=0, cat=0, sleep=0)
| Shah Rukh Khan | Salman Khan | Akshay Kumar | Aamir Khan | Hrithik Roshan | isPartial | |
|---|---|---|---|---|---|---|
| date | ||||||
| 2021-09-01 00:00:00 | 0 | 4 | 11 | 0 | 4 | False |
| 2021-09-01 01:00:00 | 3 | 13 | 5 | 3 | 3 | False |
| 2021-09-01 02:00:00 | 7 | 13 | 12 | 5 | 3 | False |
| 2021-09-01 03:00:00 | 3 | 24 | 17 | 3 | 13 | False |
| 2021-09-01 04:00:00 | 6 | 17 | 15 | 6 | 16 | False |
| ... | ... | ... | ... | ... | ... | ... |
| 2021-09-29 20:00:00 | 0 | 14 | 9 | 4 | 9 | False |
| 2021-09-29 21:00:00 | 5 | 9 | 6 | 5 | 3 | False |
| 2021-09-29 22:00:00 | 2 | 10 | 13 | 4 | 12 | False |
| 2021-09-29 23:00:00 | 4 | 16 | 7 | 4 | 8 | False |
| 2021-09-30 00:00:00 | 4 | 18 | 6 | 4 | 4 | False |
701 rows × 6 columns
What does the above data frame tells us? it tells us the hourly search of these actors for example on 2021-09-01 02:00:00, shah rukh khan was searched 7 times, similarly salman khan was searched 13 times in an hour
Now I want see the search results country wise
by_region = pytrends.interest_by_region(resolution='COUNTRY', inc_low_vol=True, inc_geo_code=False)
by_region.head(10)
| Shah Rukh Khan | Salman Khan | Akshay Kumar | Aamir Khan | Hrithik Roshan | |
|---|---|---|---|---|---|
| geoName | |||||
| Afghanistan | 10 | 69 | 10 | 11 | 0 |
| Albania | 0 | 0 | 0 | 0 | 0 |
| Algeria | 0 | 0 | 0 | 0 | 100 |
| American Samoa | 0 | 0 | 0 | 0 | 0 |
| Andorra | 0 | 0 | 0 | 0 | 0 |
| Angola | 0 | 100 | 0 | 0 | 0 |
| Anguilla | 0 | 0 | 0 | 0 | 0 |
| Antarctica | 0 | 0 | 0 | 0 | 0 |
| Antigua & Barbuda | 0 | 0 | 0 | 0 | 0 |
| Argentina | 0 | 100 | 0 | 0 | 0 |
The above data shows the searches by country wise,
# by region greater than 10 searches
by_region[by_region["Shah Rukh Khan"] > 40]
| Shah Rukh Khan | Salman Khan | Akshay Kumar | Aamir Khan | Hrithik Roshan | |
|---|---|---|---|---|---|
| geoName | |||||
| Belgium | 66 | 18 | 0 | 0 | 16 |
| Canada | 41 | 25 | 18 | 8 | 8 |
| China | 100 | 0 | 0 | 0 | 0 |
| Costa Rica | 100 | 0 | 0 | 0 | 0 |
| Croatia | 100 | 0 | 0 | 0 | 0 |
| Egypt | 46 | 8 | 15 | 0 | 31 |
| Finland | 50 | 0 | 50 | 0 | 0 |
| Georgia | 50 | 0 | 0 | 0 | 50 |
| Guyana | 67 | 33 | 0 | 0 | 0 |
| Indonesia | 54 | 19 | 9 | 7 | 11 |
| Kenya | 71 | 16 | 5 | 0 | 8 |
| Malaysia | 62 | 21 | 6 | 4 | 7 |
| Maldives | 60 | 20 | 0 | 20 | 0 |
| Philippines | 53 | 9 | 4 | 34 | 0 |
| Réunion | 100 | 0 | 0 | 0 | 0 |
| Senegal | 100 | 0 | 0 | 0 | 0 |
| Singapore | 57 | 19 | 11 | 5 | 8 |
| Somalia | 50 | 17 | 33 | 0 | 0 |
| Spain | 60 | 21 | 19 | 0 | 0 |
| Sri Lanka | 48 | 18 | 7 | 11 | 16 |
| Taiwan | 50 | 0 | 0 | 50 | 0 |
| Thailand | 65 | 18 | 11 | 6 | 0 |
| Trinidad & Tobago | 62 | 7 | 7 | 8 | 16 |
| Tunisia | 67 | 0 | 0 | 33 | 0 |
| Ukraine | 100 | 0 | 0 | 0 | 0 |
| Venezuela | 50 | 0 | 50 | 0 | 0 |
Pytrends can also help you find keywords that are closely tied to a primary keyword of your choice and then return a list of related keywords shown on Google Trends. Let us find a list of related queries for “machine learning” and return the top queries.
data = pytrends.related_queries()
data['Salman Khan']['top']
| query | value | |
|---|---|---|
| 0 | salman khan movie | 100 |
| 1 | shahrukh khan | 74 |
| 2 | salman khan age | 59 |
| 3 | salman khan movies | 44 |
| 4 | bigg boss | 43 |
| 5 | salman khan song | 37 |
| 6 | salman khan net worth | 31 |
| 7 | aryan khan | 30 |
| 8 | akshay kumar | 30 |
| 9 | salman khan height | 27 |
| 10 | katrina kaif | 25 |
| 11 | bigg boss 15 | 24 |
| 12 | salman khan wife | 24 |
| 13 | salman khan photo | 24 |
| 14 | aamir khan | 23 |
| 15 | salman khan news | 23 |
| 16 | salman yusuff khan | 22 |
| 17 | amitabh bachchan | 20 |
| 18 | srk | 17 |
| 19 | salman khan height in feet | 16 |
| 20 | wanted salman khan | 16 |
| 21 | salman khan son | 16 |
| 22 | hrithik roshan | 15 |
| 23 | salman khan instagram | 14 |
| 24 | salman khan new movie | 14 |
The above are the related queries and keywords searched about salman khan
data['Shah Rukh Khan']['top']
| query | value | |
|---|---|---|
| 0 | shah rukh khan son | 100 |
| 1 | aryan khan | 35 |
| 2 | shahrukh khan | 14 |
| 3 | shah rukh khan movies | 13 |
| 4 | shah rukh khan age | 13 |
| 5 | india | 12 |
| 6 | shah rukh khan son drugs | 10 |
| 7 | shah rukh khan net worth | 9 |
| 8 | gauri khan | 9 |
| 9 | shah rukh khan son age | 8 |
| 10 | salman khan | 8 |
| 11 | shah rukh khan wife | 7 |
| 12 | shah rukh khan daughter | 6 |
| 13 | shah rukh khan son arrested | 5 |
| 14 | shah rukh khan family | 5 |
| 15 | anak shah rukh khan | 5 |
| 16 | srk | 5 |
| 17 | shah rukh khan son news | 5 |
| 18 | suhana khan | 5 |
| 19 | आर्यन खान | 4 |
| 20 | shahrukh khan son | 4 |
| 21 | shah rukh khan children | 4 |
| 22 | ncb | 4 |
| 23 | aryan khan age | 4 |
| 24 | kareena kapoor | 4 |
data['Aamir Khan']['top']
| query | value | |
|---|---|---|
| 0 | salman khan | 100 |
| 1 | shahrukh khan | 87 |
| 2 | aamir khan movies | 86 |
| 3 | amir khan | 70 |
| 4 | aamir khan wife | 60 |
| 5 | aamir khan height | 51 |
| 6 | akshay kumar | 49 |
| 7 | aamir khan age | 49 |
| 8 | aamir khan filmleri | 47 |
| 9 | samantha | 39 |
| 10 | aamir khan actor | 37 |
| 11 | hrithik roshan | 37 |
| 12 | aamir khan height in feet | 36 |
| 13 | dangal | 32 |
| 14 | aamir khan net worth | 29 |
| 15 | naga chaitanya | 29 |
| 16 | aamir khan son | 27 |
| 17 | aamir khan daughter | 27 |
| 18 | aamir khan divorce | 25 |
| 19 | saif ali khan | 25 |
| 20 | amitabh bachchan | 23 |
| 21 | aamir khan wife name | 21 |
| 22 | athar aamir khan | 19 |
| 23 | salman khan net worth | 18 |
| 24 | shah rukh khan | 18 |
Lets give some comparison of Google itself, Youtube, Facebook, Instagram and twitter
kw_list = ['google','youtube','facebook','instagram','twitter']
pytrends.build_payload(kw_list, cat=0, timeframe='today 12-m')
data = pytrends.interest_over_time() # this function will get all the historical data of the entered word
data = data.reset_index()
fig = px.line(data, x="date", y= kw_list , title='Keyword Web Search Interest Over Time')
fig.show()
Insta and twitter are below the line and Google is on top